<?php
if (@$_POST['open'] != 'please') {
    exit;
}
include_once("../../php/include_all.php");
?>

<div class="row-fluid sortable">
	<div class="box span12 geo-box">
    <div class="box-header well">
    	 <h5><i class="icon-calendar"></i> Master Berita</h5></div>
    <div class="box-content">
        <form class="form-search">
        	<div class="input-append">
            	<input type="text" id='cariData' class='span8' name='cari' />
                <a class='btn btn-primary2 btn-Cari'><div id='loader' style='margin-top:2px'></div>Cari</a>
			</div>
        </form>
        
		<a href="#modalwin" data-toggle="modal" class="btn btn-small btn-primary2 no-radius btn-tambah"><i class="icon-plus"></i> Tambah Data</a>
   </div>
   <div id="modalwin" class="modal hide fade" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
		<header class="modal-header">
			<a href="#" class="close" data-dismiss="modal">x</a>
			<h3>Data Berita</h3>
		</header>
        <div class="modal-body">
    	<form id="form_berita">
        <input type="hidden" name="act" value="simpan_berita" />
        <input type="hidden" name="id_berita" id="id_berita" value="0" />
        	<table class="table-form">
            	<tbody>
                	<tr>
                    	<td>Judul</td><td>:</td><td><input type="text" name="judul" id="judul" class="span8" /></td>
                    </tr>
                    <tr>
                    	<td>Pengantar</td><td>:</td><td><textarea class="input-xlarge" rows="2" name="pengantar" id="pengantar"></textarea></td>
                    </tr>
                    <tr>
                    	<td>Isi</td><td>:</td><td><textarea class="input-xlarge" rows="5" name="isi" id="isi"></textarea></td>
                    </tr>
                	<tr>
                    	<td></td>
						<td></td>
						<td>
							 <a class="btn btn-primary2 btn-small bt-simpan-berita"><div id="loader"></div><i class="icon-save"></i>&nbsp;Simpan</a><span id="result"></span>

						</td>	
                    </tr>
                </tbody>
            </table>
        </form>
        </div></div>
        <div class="box-content">
        <table class="table table-bordered" align="center" width="100%">
       		<thead>
            	<tr>
                	<th width="5%">NO</th>
                	<th width="18%">JUDUL</th>
                	<th width="20%">PENGANTAR</th>
                	<th width="40%">ISI</th>
                	<th width="17%">AKSI</th>
                </tr>
            </thead>
            <tbody id="tbody_berita">
            <?php
			$x=1;
			$berita = get_datas("select * from mus_berita order by id_berita");
			foreach($berita as $berita){
			?>
            <tr>
				<td><?php echo $x ?></td>
            	<td><?php echo $berita['judul']?></td>
                <td><?php echo $berita['pengantar']?></td>
                <td><?php echo $berita['isi']?></td>
				<td style="text-align:center;">
					<a href="#modalwin" data-toggle="modal" class="btn btn-info btn-small bt-edit-berita" name="<?php echo $berita['id_berita']; ?>"><i class="icon-edit icon-white"></i>Ubah</a>
                    <a class="btn btn-danger btn-small bt-delete-berita" name="<?php echo $berita['id_berita']; ?>"><i class="icon-trash icon-white"></i>Hapus</a>
                    </td>
            </tr>
            <?php
				$x++;
			} 
			?></tbody>
        </table>
        </div>
    </div>
</div>
<script>
//$('#some-textarea').wysihtml5();
var urls = 'admin/master_informasi/aksi_simpan.php';
$('.bt-simpan-berita').click(function(){
	var btn = $(this);
	var load = $('#loader');
	var rslt = $('#result');
	load.addClass('icon-spinner icon-spin icon-2x white');
//	btn.removeClass('btn-primary').addClass('btn-info');
	
	var form =$('#form_berita');
	var data = form.serializeArray();
	var post = $.post(urls,data);
	post.done(function(res){
		var result = res.split('__');
		if(result.length==3){
			if(result[0]=='success'){
				rslt.html('<font color="green">Data Tersimpan...</font>');
				setTimeout(function(){
					rslt.html('');
				},1500);
				
				var tbody = $('#tbody_berita');
				tbody.html(result[2]);
				init();
				document.getElementById('id_berita').value='0';
				document.getElementById('judul').value='';
				document.getElementById('pengantar').value='';
				document.getElementById('isi').value='';
				$('#modalwin').modal('hide');
			}else{
				rslt.html('<font color="red">'+res+'</font>');
			}
		}else{
			rslt.html('<font color="red">'+res+'</font>');
		}
		load.removeClass();
		btn.removeClass('btn-info').addClass('btn-primary2');
	});
});

$('.btn-tambah').click(function(){
	document.getElementById('id_berita').value='0';
				document.getElementById('judul').value='';
				document.getElementById('pengantar').value='';
				document.getElementById('isi').value='';
});

function init(){
	$('.bt-delete-berita').click(function(){
		var btn  = $(this);
		var pid  = this.name;
		var load = $(this);
		 load.html('<div class="icon-spinner icon-spin icon-2x white"></div>');
		bootbox.confirm("Anda akan menghapus berita ini?", function(result) {
			if(result==true){
				if(pid!=0){
					var post = $.post(urls,{act:"del_berita",idberita:pid});
					post.done(function(res){
						
						var hasil = res.split('__');
						if(hasil.length == 2){
							if(hasil[0] == 'success'){
								var tbody = $('#tbody_berita');
								tbody.html(hasil[1]);
								
								init();
							}
						}
					});
					load.html('<i class="icon-trash icon-white"></i> hapus</a> ');
				}
			}else{
				load.html('<i class="icon-trash icon-white"></i> hapus</a> ');
			}
		});
	});
	$('.bt-edit-berita').click(function(){
		var idberita = this.name;
		var form = $('#form_berita');
		var post = $.post(urls,{act:'edit_berita',idberita:idberita});
		post.done(function(res){
		
		console.log(res);
			var value = res.split('__');
			$('#id_berita').val(value[0]);
			form.find('input[name="judul"]').val(value[1]);
			form.find('textarea[name="pengantar"]').val(value[2]);
			form.find('textarea[name="isi"]').val(value[3]);
				//document.getElementById('judul').value='';
				//document.getElementById('pengantar').value='';
				//document.getElementById('isi').value='';
				//$('#modalwin').modal('hide');
		});
	});
}

$('.btn-Cari').click(function(){
	var cari = $('#cariData').val();
	source = "admin/master_informasi/cari.php?car="+cari;
	var caridt = $.get(source);
	caridt.done(function(data){
		$('#tbody_berita').html(data);
		$('#cariData').val(" ");
	});
});
init();
</script>
 